App.controller('mtApplyController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {
    Page.ajaxPost('employeeGroup/empTree.do', null, function(resp) {
        $scope.empList = resp.data;
    });

    $scope.grid = {
        options : {
          bAutoWidth : false
        },
        add : function() {
            $scope.updateFlag = false;
            $scope.maintenanceApply = {};
            $scope.maintenanceApply.mediaType = "1";//车辆类型
            $scope.busList = [];
            $scope.applyModal.show();
        },
        update: function(row) {
            $scope.updateFlag = true;
            $scope.maintenanceApply = row;
            $scope.maintenanceApply.bus = {};
            $scope.maintenanceApply.bus.busId = $scope.maintenanceApply.mediaId;
            $scope.maintenanceApply.bus.vehicleNo = $scope.maintenanceApply.vehicleNo;
            $scope.maintenanceApply.bus.vehicleNoAbbr = $scope.maintenanceApply.vehicleNoAbbr;
            $scope.maintenanceApply.bus.plateNo = $scope.maintenanceApply.plateNo;
            $scope.maintenanceApply.bus.lineNo = $scope.maintenanceApply.lineNo;
            $scope.maintenanceApply.bus.companyName = $scope.maintenanceApply.companyName;
            $scope.maintenanceApply.bus.customerId = $scope.maintenanceApply.customerId;
            $scope.maintenanceApply.bus.customerName = $scope.maintenanceApply.customerName;
            $scope.maintenanceApply.bus.adName = $scope.maintenanceApply.adName;
            $scope.maintenanceApply.bus.publishType = $scope.maintenanceApply.publishType;
            $scope.maintenanceApply.bus.makeType = $scope.maintenanceApply.makeType;
            $scope.maintenanceApply.bus.stickerType = $scope.maintenanceApply.stickerType;
            $scope.maintenanceApply.bus['show_' + $scope.maintenanceApply.mediaId] = true;
            $scope.busList = [];
            $scope.busList.push($scope.maintenanceApply.bus);
            $scope.applyModal.show();
        },
        save : function() {
            $scope.validateForm.doValidate();
            if($scope.validateForm.$valid) {
                if($scope.maintenanceApply.bus.busId == undefined || $scope.maintenanceApply.bus.busId == "") {
                    Page.notice("请选择报修媒体资源!");
                    return;
                }
                Page.ajaxPost($scope.maintenanceApply.maintenanceId ? 'maintenanceApply/update.do' : 'maintenanceApply/add.do', $scope.maintenanceApply, function() {
                    $scope.grid.query();
                    $scope.grid.cancel();
                });
            }
        },
        deal : function(row) {
            $scope.maintenanceDeal = row;
            $scope.maintenanceDeal.bus = {};
            $scope.maintenanceDeal.bus.busId = $scope.maintenanceDeal.mediaId;
            $scope.maintenanceDeal.bus.vehicleNo = $scope.maintenanceDeal.vehicleNo;
            $scope.maintenanceDeal.bus.vehicleNoAbbr = $scope.maintenanceDeal.vehicleNoAbbr;
            $scope.maintenanceDeal.bus.plateNo = $scope.maintenanceDeal.plateNo;
            $scope.maintenanceDeal.bus.lineNo = $scope.maintenanceDeal.lineNo;
            $scope.maintenanceDeal.bus.companyName = $scope.maintenanceDeal.companyName;
            $scope.maintenanceDeal.bus.customerId = $scope.maintenanceDeal.customerId;
            $scope.maintenanceDeal.bus.customerName = $scope.maintenanceDeal.customerName;
            $scope.maintenanceDeal.bus.adName = $scope.maintenanceDeal.adName;
            $scope.maintenanceDeal.bus.publishType = $scope.maintenanceDeal.publishType;
            $scope.maintenanceDeal.bus.makeType = $scope.maintenanceDeal.makeType;
            $scope.maintenanceDeal.bus.stickerType = $scope.maintenanceDeal.stickerType;
            //初始化选中的任务集合
            $scope.checkedStepList = [];
            Page.ajaxPost('employeeGroup/groupList.do', null, function(resp) {
                $scope.groupList = resp.data;
                $scope.workflow = {};
                var stepList = new Array();
                stepList.push({'taskName' : '裁图','taskType' : "1"});
                stepList.push({'taskName' : '打印','taskType' : "2"});
                stepList.push({'taskName' : '覆膜','taskType' : "3"});
                $scope.workflow.steps = stepList;
                $scope.maintenanceDeal.publishTask = {};
                $scope.dealModal.show();
            });

        },

        dealSave : function() {
            if($scope.maintenanceDeal.publishTask.groupId == undefined || $scope.maintenanceDeal.publishTask.groupId == ""
                || $scope.maintenanceDeal.publishTask.limitTime == undefined || $scope.maintenanceDeal.publishTask.limitTime == "") {
                Page.notice("请填写工作组、截止时间!");
                return;
            }

            if($scope.showStickerItem) { //选择了胶贴制作任务
                for (var i = 0; i < $scope.workflow.steps.length; i++) {
                    var obj = $scope.workflow.steps[i];
                    if(obj.id == undefined || obj.id == ""
                        || obj.limitTime == undefined || obj.limitTime == "") {
                        Page.notice("请填写办理人、截止时间!");
                        return;
                    }
                }
            }

            Page.ajaxPost('maintenanceApply/deal.do', {maintenanceId : $scope.maintenanceDeal.maintenanceId,
                publishTask : $scope.maintenanceDeal.publishTask,showStickerItem : $scope.showStickerItem,
                stepList : $scope.workflow.steps}, function(resp) {
                Page.notice("操作成功!");
                $scope.dealCancel();
                $scope.grid.query();
            });
        },
        view : function(row) {
            $scope.maintenanceView = row;
            $scope.maintenanceView.bus = {};
            $scope.maintenanceView.bus.busId = $scope.maintenanceView.mediaId;
            $scope.maintenanceView.bus.vehicleNo = $scope.maintenanceView.vehicleNo;
            $scope.maintenanceView.bus.vehicleNoAbbr = $scope.maintenanceView.vehicleNoAbbr;
            $scope.maintenanceView.bus.plateNo = $scope.maintenanceView.plateNo;
            $scope.maintenanceView.bus.lineNo = $scope.maintenanceView.lineNo;
            $scope.maintenanceView.bus.companyName = $scope.maintenanceView.companyName;
            $scope.maintenanceView.bus.customerId = $scope.maintenanceView.customerId;
            $scope.maintenanceView.bus.customerName = $scope.maintenanceView.customerName;
            $scope.maintenanceView.bus.adName = $scope.maintenanceView.adName;
            $scope.maintenanceView.bus.publishType = $scope.maintenanceView.publishType;
            $scope.maintenanceView.bus.makeType = $scope.maintenanceView.makeType;
            $scope.maintenanceView.bus.stickerType = $scope.maintenanceView.stickerType;
            Page.ajaxPost('maintenanceApply/view.do', {maintenanceId : row.maintenanceId}, function(resp) {
                $scope.maintenanceView.taskList = resp.data;
                $scope.viewModal.show();
            });
        },
        completeApply : function(row) {
            Page.confirm("确定要完成吗？", function() {
                Page.ajaxPost('maintenanceApply/completeApply.do',{maintenanceId : row.maintenanceId}, function(resp){
                    Page.notice("操作成功!");
                    $scope.grid.query();
                });
            });

        },
        column_defs: [{
            target : 11,
            buttons : [
                {
                    name: '编辑',
                    class: 'btn-warning btm-sm',
                    icon: 'fa-edit',
                    event: 'update',
                    visiable : function(row) {
                        if(row.status == '1') {
                            return true;
                        } else {
                            return false;
                        }
                    }
                },
                {
                    name: '删除',
                    class: 'btn-danger',
                    icon: 'fa-close',
                    event: function(row) {
                        Page.confirm("确定要删除吗？", function() {
                            Page.ajaxPost('maintenanceApply/delete.do', row, function() {
                                $scope.grid.query();
                            });
                        });
                    },
                    visiable : function(row) {
                        if(row.status == '1') {
                            return true;
                        } else {
                            return false;
                        }
                    }
                },
                {
                    name: '办理',
                    class: 'btn-info',
                    icon: 'glyphicon glyphicon-leaf',
                    event: 'deal',
                    visiable : function(row) {
                        if(row.status == '1') {
                            return true;
                        } else {
                            return false;
                        }
                    }
                },
                {
                    name: '查看',
                    class: 'btn-info',
                    icon: 'glyphicon glyphicon-eye-open',
                    event: 'view',
                    visiable : function(row) {
                        if(row.status == '2') {
                            return true;
                        } else {
                            return false;
                        }
                    }
                },
                {
                    name: '完成',
                    class: 'btn-danger',
                    icon: 'glyphicon glyphicon-exclamation-sign',
                    event: 'completeApply',
                    visiable : function(row) {
                        if(parseInt(row.notCompleteTaskCount) == 0 && row.status == '2') {
                            return true;
                        } else {
                            return false;
                        }
                    }
                }
            ]
        }]
    };

    $scope.dealCancel = function() {
        $scope.maintenanceDeal.publishTask = {};
        $scope.dealModal.hide();
    };

    $scope.applyModal = {
        onOkClick : function() {
            $scope.grid.save();
        },
        onHidden : function() {
            $scope.applyModal.hide();
        }
    }

    $scope.busObj = {
        query : function() {
            Page.ajaxPost('maintenanceApply/queryBus.do', $scope.busObj, function(resp){
                $scope.busList = resp.data;
                if($scope.updateFlag) { //回显选中checkbox
                    for (var i = 0; i < $scope.busList.length; i++) {
                        var obj = $scope.busList[i];
                        if(obj.busId == $scope.maintenanceApply.bus.busId) {
                            obj['show_' + obj.busId] = true;
                        }
                    }
                }
            })
        },
        reset : function() {
            $scope.busObj.vehicleNo = "";
            $scope.busObj.vehicleNoAbbr = "";
            $scope.busObj.plateNo = "";
            $scope.busObj.query();
        },
        click : function(bus, event) {
            $("input[name='checkBus']").removeAttr("checked");
            $(event.currentTarget).attr("checked","true");
            //清除已有红色背景样式
            $(event.currentTarget).parent().parent().parent().find("tr").each(function(i,tr){
                $(tr).css("color","#333333");
            });
            $(event.currentTarget).parent().parent().css("color","red");
            $scope.maintenanceApply.bus = angular.extend({},bus);
        }

    };

    $scope.clickGroup = function(group) {
        $scope.maintenanceDeal.groupId = group.groupId;
    };

    $scope.selectable = function(data) {
        if(data.id.indexOf("_") != -1) { //选择的部门
            return false;
        }
    };

    //选择胶贴制作checkbox
    $scope.clickSticker = function(event) {
        if($(event.currentTarget)[0].checked) {
            $scope.showStickerItem = true;
            $(".empListDiv").each(function(i,data){
                $(data).find("input").removeAttr("disabled").css("border-color","#e8750b");
            })
        } else {
            $(".empListDiv").each(function(i,data){
                $(data).find("input").attr("disabled","true").css("border-color","");
            });
            $scope.showStickerItem = false;
        }
    };

    //客户集合
    $scope.queryCustomerList = function() {
        Page.ajaxPost('maintenanceApply/customerList.do', null, function(resp){
            $scope.customerList = resp.data;
        })
    };

    $scope.queryCustomerList();


}]);